<?php
require_once 'BaseAction.php';

class AdminController extends TT_Base_Action
{
    public function indexAction()
    {
        $u = $this->getUser();
        $this->view->user = $u;
        // Kiem tra co phai la admin
        if($u->admin !=1){
            $this->_helper->getHelper('Redirector')->gotoUrlAndExit('user/login');
        }
    }
    
    // Them quyen quan tri cho user
    public function addmoderatorAction()
    {
        // Tham so
        $m_model = $this->loadModel('Modes');  
        $num1=$m_model->showoverdue_num();
        $this->view->num1=$num1[0]->num;
        
        $num2=$m_model->showmaturity_num();
        $this->view->num2=$num2[0]->num;
        
        $params=$this->_getAllParams(); 
        $page=$params['page'];
        $page_rows=5;
        $page_start=($page-1)*$page_rows;
        $this->view->test12=$params;
        
        $u = $this->getUser();
        $this->view->user = $u;
        if($u->admin !=1){
            $this->_helper->getHelper('Redirector')->gotoUrlAndExit('user/login');
        }
        $u_model = $this->loadModel("Admins");
        if($this->getRequest()->isPost())
        {
            $username = trim($this->_getParam('username'));
            $a = $u_model->addmoderator($username);
            
            $this->view->name = $username;
            $b = $u_model->checkInfo($username);
      
            if($b->level == 1){
                $this->view->thongbao1 = 1;
            }
        }
        $mod = $u_model->moderator();
        $this->view->mod = $mod;
        
        //$mem = $u_model->members();
        //$this->view->mem = $mem;
        $mem = $u_model->members($page_start,$page_rows);
        $this->view->mem = $mem;
        $num=$u_model->members_num();
        $this->view->num = $num;
    }
    
    // Bo quyen quan tri cua user
    public function removemoderatorAction()
    {
        // Tham so
        $m_model = $this->loadModel('Modes');  
        $num1=$m_model->showoverdue_num();
        $this->view->num1=$num1[0]->num;
        
        $num2=$m_model->showmaturity_num();
        $this->view->num2=$num2[0]->num;
        
        $params=$this->_getAllParams(); 
        $page=$params['page'];
        $page_rows=5;
        $page_start=($page-1)*$page_rows;
        $this->view->test12=$params;
        
        $u = $this->getUser();
        $this->view->user = $u;
        if($u->admin !=1){
            $this->_helper->getHelper('Redirector')->gotoUrlAndExit('user/login');
        }
        $u_model = $this->loadModel("Admins");
        if($this->getRequest()->isPost())
        {
            $username = trim($this->_getParam('username'));
            $a = $u_model->removemoderator($username);
            
            $this->view->name = $username;
            $b = $u_model->checkInfo($username);
            if($b->level == 0){
                $this->view->thongbao = 1;
            }
        }
        $mod = $u_model->moderator();
        $this->view->mod = $mod;
        
        //$mem = $u_model->members();
        //$this->view->mem = $mem;
        $mem = $u_model->members($page_start,$page_rows);
        $this->view->mem = $mem;
        $num=$u_model->members_num();
        $this->view->num = $num;
    }
    
    // xoa user va quan tri vien
    public function removeuserAction()
    {
        // Tham so
        $m_model = $this->loadModel('Modes');  
        $num1=$m_model->showoverdue_num();
        $this->view->num1=$num1[0]->num;
        
        $num2=$m_model->showmaturity_num();
        $this->view->num2=$num2[0]->num;
        
        $params=$this->_getAllParams(); 
        $page=$params['page'];
        $page_rows=5;
        $page_start=($page-1)*$page_rows;
        $this->view->test12=$params;
        
        $u = $this->getUser();
        $this->view->user = $u;
        if($u->admin !=1){
            $this->_helper->getHelper('Redirector')->gotoUrlAndExit('user/login');
        }
        $u_model = $this->loadModel('Admins');
        if($this->getRequest()->isPost())
        {
            $username = trim($this->_getParam('username'));
            $a = $u_model->removeuser($username);
            
            $this->view->name = $username;
            $this->view->thongbao = 1;
        }
        $mod = $u_model->moderator();
        $this->view->mod = $mod;
        
        //$mem = $u_model->members();
        //$this->view->mem = $mem;
        $mem = $u_model->members($page_start,$page_rows);
        $this->view->mem = $mem;
        $num=$u_model->members_num();
        $this->view->num = $num;
        
    }
}
?>
